import model
import random
import datetime
import types


def genBorrowers(num):
    borrowers = []
    for i in range(num):
        borrower = model.Borrower()
        fname = "First Name" + str(random.randint(0, num))
        lname = "Last Name" + str(random.randint(0, num))
        date = datetime.date.today() - datetime.timedelta(days=random.randint(0, 2*365))
        card = model.Card()
        card.id = 1000 + i
        card.holder = fname + " " + lname
        card.issuedTime = date
        card.expiredTime = date + datetime.timedelta(days=4*365)
        card.balance = 1000 * random.random()

        borrower.firstName = fname
        borrower.lasteName = lname
        borrower.card = card
        borrower.maxIssuedBooks = 15
        borrower.issuedBooks = []
        borrower.dailyFee = 1.0
        borrower.sex = "Male" if random.randint(0, 9) % 2 == 0 else "Female"
        borrower.birthday = date
        borrower.address = "Address" + str(random.randint(0, 1000))
        borrower.phoneNumber = "PhoneNumber" + str(random.randint(0, 1000))

        borrowers.append(borrower)

    return borrowers


def genManager(num):
    managers = []
    for i in range(num):
        fname = "First Name" + str(random.randint(0, num))
        lname = "Last Name" + str(random.randint(0, num))
        manager = model.Manager()
        manager.salary = 10000 * random.random()
        manager.onDuty = (i == num - 1)
        manager.firstName = fname
        manager.lastName = lname
        managers.append(manager)

    return managers


def genBooks(num):
    books = []
    for i in range(num):
        book = model.BookItem()
        book.barCode = i + 10000
        book.ISBN = "ISBN" + str(i)
        book.title = "Title" + str(random.randint(0, 1000))
        book.catagory = "Catagory" + str(random.randint(0, 1000))
        book.author = "Author" + str(random.randint(0, 1000))
        book.publisher = "Publisher" + str(random.randint(0, 1000))
        book.publicationDate = datetime.date.today() - datetime.timedelta(days=random.randint(0, 20*365))
        book.pages = random.randint(100, 1000)
        book.price = 200 * random.random()
        books.append(book)

    return books


def obj2String(obj, extra='    '):
    return str(obj.__class__) + '\n' + '\n'.join(
        (extra + (str(item) + ' = ' +
                  (obj2String(obj.__dict__[item], extra + '    ') if hasattr(obj.__dict__[item], '__dict__') else str(
                      obj.__dict__[item])))
         for item in sorted(obj.__dict__)))


def main():
    library = model.Library
    library.managers = []
    library.bookItems = []
    library.borrowers = []
    library.issues = []

    library.name = "BJ Library"
    library.location = "Beijing"
    library.totalSits = 155
    library.availableSits = 155
    library.openTime = datetime.time(8)
    library.closeTime = datetime.time(17)

    borrowerList = genBorrowers(10)
    managerList = genManager(10)
    bookList = genBooks(10)

    library.borrowers.append(borrowerList[0])
    library.managers.append(managerList[0])
    library.bookItems.append(bookList[0])

    library.occupySit(library)
    print('availableSits: ' + str(library.availableSits))
    library.releaseSit(library)
    print('availableSits: ' + str(library.availableSits))

    print(library.isOpen(library))

    print(len(library.searchBooks(library, barCode=10000)))
    library.addBookItem(library, bookList[1])

    print(len(library.searchBooks(library, ISBN="ISBN1")))
    library.removeBookItem(library, bookList[1])

    print(obj2String(library.searchBorrower(library, borrowerList[0].card.id)))
    library.addBorrower(library, borrowerList[1])
    library.removeBorrower(library, borrowerList[0])

    library.addManager(library, managerList[1])
    library.removeManager(library, managerList[0])

    manager = managerList[9]
    manager.issueBook(borrowerList[1], bookList[0])
    manager.receiveBook(bookList[0])

    for borrower in library.borrowers:
        print(obj2String(borrower))

    for manager in library.managers:
        print(obj2String(manager))

    for bookItem in library.bookItems:
        print(obj2String(bookItem))


if __name__ == '__main__':
    main()
